var main_vm = new Vue({
    el: 'main',
    data: {
        list: [],
        formData: {
            id: Url.queryString('id'),
            ids: [],
            values: []
        }
    },
    computed: {
        token() {
            return localStorage.token || Cookies.get('token')
        }
    },
    watch: {
        keywords(val) {
            if (val == "" || val == null) {
                this.query = null
            }
        },
        list(val) {
            if (val.length) {
                for (var i = 0; i < val.length; i++) {
                    this.formData.ids.push(this.list[i].id)
                }

                for (var j = 0; j < val.length; j++) {
                    var a = null
                    this.formData.values.push(a)
                }
            }
        }
    },
    methods: {
        getList() {
            var self = this
            axios.get(window.config.host + '/score_user_list', {
                    headers: {
                        Authorization: 'Bearer ' + this.token
                    },
                    params: {
                        id: this.id
                    }
                })
                .then(function(res) {
                    console.log(res)
                    if (res.status == 200) {
                        self.list = res.data.data
                    } else {
                        alert(res.data.message)
                    }
                })
                .catch(function(res) {

                })
        },
        checkForm() {
            if (_.indexOf(this.formData.values, null) == -1 && _.indexOf(this.formData.values, "") == -1) {
                var r = /^([1]?\d{1,2})$/
                for(i in this.formData.values){
                    if(!r.test(this.formData.values[i])){
                        alert('填写的分数必须为0-100的正整数');
                        return false;
                    }
                }
                $("#modal-hint").modal('show')
            } else {
                alert('评分未完成，请为每个职工评分')
            }
        },
        checkInput(index) {
            var value = event.currentTarget.value
            var r = /^([1]?\d{1,2})$/
            if(!r.test(value)){
                alert('评分必须为0-100之间的整数')
                event.currentTarget.value = ''
                this.formData.values[index] = ''
            }
        },
        submitForm() {
            var target = event.currentTarget
            $(target).button('loading');
            var self = this
            axios.post(window.config.host + '/scores', this.formData, {
                    headers: {
                        Authorization: 'Bearer ' + this.token
                    }
                })
                .then(function(res) {
                    if (res.status == 200) {
                        $("#modal-hint").modal('hide')
                        alert('提交成功')
                        window.location.href = "/years"
                    }else{
                        alert(res.data.message)
                    }
                })
                .catch(function(err) {

                })
                .then(function() {
                    $(target).button('reset');
                })
        }
    },
    mounted() {
        this.getList()
    }
})